package com.hcb.micro.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.RegionalAgent;
import com.hcb.micro.service.ICityService;
import com.hcb.micro.service.IDistrictService;
import com.hcb.micro.service.IProvinceService;
import com.hcb.micro.service.IRegionalAgentService;
import com.hcb.micro.service.IUnionConsumeService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("regionalAgent")
public class RegionalAgentController extends BaseControllers{
	
	@Autowired
	IRegionalAgentService regionalAgentService;
	
	@Autowired
	ICityService cityService;
	@Autowired
	IProvinceService provinceService;
	@Autowired
	IDistrictService districtService;
	
	/**
	 * 区域代理申请
	 * @return
	 */
	@RequestMapping(value = "listApplyRegionalAgent", method = RequestMethod.POST)
	@ResponseBody
	public String listApplyRegionalAgent() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			para.put("accountId", bodyInfo.getString("accountId"));
		}
		
		if (bodyInfo.get("status") != null && !"".equals(bodyInfo.get("status"))) {
			int status=Integer.parseInt(bodyInfo.getString("status")+"");
			if(status!=4){
				para.put("status", bodyInfo.getString("status"));
			}
		}
		
		List<RegionalAgent> dListAll =regionalAgentService.selectByPrimaryKey(para);
		
		
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<RegionalAgent> dList =regionalAgentService.selectByPrimaryKey(para);
		
		if(dList.size() <= 0){
			json.put("result", "1");
			json.put("description", "商家列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,Object>> list = new ArrayList<>();
		for(RegionalAgent d:dList){
			
			Map<String,Object> map = new HashMap<>();
			map.put("id", d.getId());
			map.put("accountId", d.getAccountId());
			map.put("status", d.getStatus());
			map.put("phone", d.getPhone());
			map.put("province",provinceService.selectByProvinceId(d.getProvince()).getName());
			map.put("city", cityService.selectByCityId(d.getCity()).getName());
			map.put("district",districtService.selectByDistrictId(d.getDistrict()).getName());
			map.put("detailadd",d.getDetailadd());
			map.put("createTime", sdf.format(d.getCreateTime()));
			list.add(map);
		}
    	json.put("result", "0");
		json.put("description", "获取商家列表成功");
		json.put("list", list);
		json.put("count",dListAll.size());
		return buildReqJsonObject(json);
	}
	
	
	@RequestMapping(value = "dealRegionalAgent", method = RequestMethod.POST)
	@ResponseBody
	public String dealRegionalAgent() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
        
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		Map<String,Object> map = new HashMap<>();
		int status=0;
		if(bodyInfo.get("id") != null && !"".equals(bodyInfo.get("id"))) {
			map.put("id",  bodyInfo.get("id"));
		}
		if(bodyInfo.get("type") != null && !"".equals(bodyInfo.get("type"))) {
			map.put("type",  bodyInfo.get("type"));
		}
		
		Map<String,Object> para = new HashMap<>();
		if((int)map.get("type")==1){
			para.put("id",map.get("id"));
			para.put("status",1);
			status=regionalAgentService.updateByPrimaryKeySelective(para);
		}
		if((int)map.get("type")==0){
			para.put("id",map.get("id"));
			para.put("status",2);
			status=regionalAgentService.updateByPrimaryKeySelective(para);
		}
		
		json.put("result", "0");
		if(status>=1){
			json.put("description", "操作成功");
		}else{
			json.put("description", "操作失败");
		}
		return buildReqJsonObject(json);
	}
	
	
}
